package com.nettest.action.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.nettest.action.BaseJsonAction;
import com.nettest.entity.system.Department;
import com.nettest.pojo.TreeVo;
import com.nettest.service.system.DeptService;
import com.nettest.util.StringUtils;
@Results({
	@Result(name = "success", location = "/system/deptManage.jsp") })
@Action("deptManage")
@Namespace("/system/dept")
public class DeptManageAction extends BaseJsonAction {

	private static final long serialVersionUID = -4740651262223282044L;

	private List<TreeVo> trees;
	private String id;
	private String deptId;
	private String deptParentId;
	private String operate;
	private Department department;
	@Autowired
	private DeptService deptService ;

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	@Action(value = "list", results = { @Result(name = "success", type = "json", params = {
			"ignoreHierarchy", "false", "excludeNullProperties", "true",
			"excludeProperties", "department,deptService","root","trees" }) })
	public String list() throws Exception  {
		try {
			if(id == null) {
				List<Department> topMenu = deptService.getSubDepts(null);
				transferVo(topMenu);
				TreeVo root = new TreeVo();
				root.setText("部门");
				root.setId("0");
				root.setState("open");
				root.setChildren(new ArrayList<TreeVo>(trees));
				trees = new ArrayList<TreeVo>();
				trees.add(root);
			} else {
				List<Department> subMenu = deptService.getSubDepts(id);
				transferVo(subMenu);
			}
		} catch (Exception e) {
			this.addActionMessage(e.getMessage());
		}
		return SUCCESS;
	}
	
	protected void transferVo(List<Department> depts) {
		trees = new ArrayList<TreeVo>();
		TreeVo treeVo = null;
		for (Department dept : depts) {
			treeVo = new TreeVo();
			treeVo.setId(dept.getDeptId());
			treeVo.setText(dept.getDeptName());
			trees.add(treeVo);
		}
	}
	@Action(value = "edit",results={@Result(name = "success", location = "/system/deptEdit.jsp")})
	public String edit() throws Exception {
		operate = StringUtils.isBlank(operate) ? "editView" : operate;
		if (!StringUtils.isBlank(deptId)) {
			department = deptService.queryDeptById(deptId);
		}
		
		if ("insertSub".equalsIgnoreCase(operate)) {
			String parentId = department.getDeptId();
			department = new Department();
			department.setParentId(parentId);
		}
		
		return SUCCESS;
	}
	@Action(value = "save",results={@Result(name = "success", location = "/system/deptEdit.jsp")})
	public String save() throws Exception {
		if ("insertSub".equalsIgnoreCase(operate)) {
			deptService.insertDept(department);
		}
		else {
			deptService.updateDept(department);
		}
		
		return SUCCESS;
	}
	@Action(value = "delete",results={@Result(name = "success", location = "/system/deptEdit.jsp")})
	public String delete() throws Exception {
		if (!StringUtils.isBlank(deptId)) {
			deptService.delDeptById(deptId);
		}
		
		return SUCCESS;
	}

	public List<TreeVo> getTrees() {
		return trees;
	}
	public void setTrees(List<TreeVo> trees) {
		this.trees = trees;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getDeptId() {
		return deptId;
	}

	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}

	public String getDeptParentId() {
		return deptParentId;
	}

	public void setDeptParentId(String deptParentId) {
		this.deptParentId = deptParentId;
	}

	public String getOperate() {
		return operate;
	}

	public void setOperate(String operate) {
		this.operate = operate;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}
	public void setDeptService(DeptService deptService) {
		this.deptService = deptService;
	}
}